//
//  WYRedDotView.m
//  LoveFreshBee
//



#import "WYRedDotView.h"

@interface WYRedDotView ()
@property(nonatomic,weak) UIImageView *imageView;
@property(nonatomic,weak) UILabel *numberLabel;
@end

@implementation WYRedDotView

static WYRedDotView *_instance;

+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    if (_instance==nil) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _instance=[super allocWithZone:zone];
        });
    }
    return _instance;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImage *img=[UIImage imageNamed:@"reddot"];
        UIImageView *imgView=[[UIImageView alloc] init];
        imgView.image=img;
        self.imageView=imgView;
        [self addSubview:imgView];
        UILabel *numberLabel=[[UILabel alloc] init];
        numberLabel.text=@"0";
        numberLabel.font=[UIFont systemFontOfSize:10];
        numberLabel.textColor=[UIColor whiteColor];
        self.numberLabel=numberLabel;
        [self addSubview:numberLabel];
        numberLabel.textAlignment=NSTextAlignmentCenter;
        self.hidden=YES;
    }
    return self;
}

+(instancetype)sharedInstance
{
    if (_instance==nil) {
        _instance=[[self alloc] init];
    }
    return _instance;
}
-(void)layoutSubviews
{
    self.imageView.frame=self.bounds;
    self.numberLabel.frame=self.bounds;
}
-(void)addToCart
{
    int num=[self.numberLabel.text intValue];
    num++;
    self.hidden=NO;
    self.numberLabel.text=@(num).stringValue;
    [UIView animateWithDuration:0.5 animations:^{
        self.transform=CGAffineTransformMakeScale(1.5, 1.5);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            self.transform=CGAffineTransformIdentity;
        } completion:nil];
        
    }];
}
-(void)reduceFromCard;
{
    
    int num=[self.numberLabel.text intValue];
    if (num>0) {
        
         num--;
    }
    if (num==0) {
        
        self.hidden=YES;
    }
   
    //self.hidden=NO;
    self.numberLabel.text=@(num).stringValue;
    [UIView animateWithDuration:0.5 animations:^{
        self.transform=CGAffineTransformMakeScale(1.5, 1.5);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            self.transform=CGAffineTransformIdentity;
        } completion:nil];
        
    }];
}

@end







